package com.ywjh.inter.servlet.doc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.entity.DocProfileEntity;
import com.ywjh.inter.entity.InstitutionEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.service.InstService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONObject;

public class Profile extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7068714440569351896L;

	protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
			throws ServletException, IOException {
	};

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "set":
			int doc_id = data.getInt("doc_id");
			UsersEntity doc = UsersService.getInstance().findUserById(doc_id);
			if (doc == null || doc.getIs_doc() != 1) {
				e = "该用户不是医生";
			} else {
				DocProfileEntity docProfileEntity = UsersService.getInstance().findDocProfileByUserId(doc_id);
				if(docProfileEntity==null) {
					docProfileEntity=new DocProfileEntity();
				}
				docProfileEntity.setStatus(1);
				docProfileEntity.setUser_id(doc_id);
				docProfileEntity.setInst_id(data.getInt("inst_id"));
				docProfileEntity.setMemo(data.getString("memo"));
				docProfileEntity.setPhone(data.getString("phone"));
				docProfileEntity.setName(data.getString("name"));
				docProfileEntity.setTitle(data.getString("title"));
				docProfileEntity.setGood(data.getString("good"));
				docProfileEntity.setIdcard(data.getString("idcard"));
				docProfileEntity.setAddress(data.getString("address"));
				docProfileEntity.setNickname(data.getString("nickname"));
				docProfileEntity.setGender(data.getInt("gender"));
				docProfileEntity.setRule_level(data.getInt("rule_level"));
				if(UsersService.getInstance().setDocProfile(docProfileEntity)) {
					r=1;
				}
			}
			break;
		case "find":
			int user_id = data.getInt("doc_id");
			UsersEntity user = UsersService.getInstance().findUserById(user_id);

			if (user==null||user.getIs_doc() == 0) {
				e = "该用户不是医生";
			} else if (user.getIs_doc() == 1) {
				DocProfileEntity docProfileEntity = UsersService.getInstance().findDocProfileByUserId(user_id);
				if (docProfileEntity == null) {
					e = "无信息";
				} else {
					r = 1;
					JSONObject profile = docProfileEntity.toJson();
					InstitutionEntity keshi = InstService.getInstance().findById(docProfileEntity.getInst_id());
					if (keshi != null) {
						profile.put("keshi", keshi.getName());
						InstitutionEntity bumen = InstService.getInstance().findById(keshi.getPid());
						if (bumen != null) {
							profile.put("bumen", bumen.getName());
							InstitutionEntity yiyuan = InstService.getInstance().findById(bumen.getPid());
							profile.put("yiyuan", yiyuan.getName());
						}
					}
					profile.put("head_url", SystemConfig.SERVER_DOMAIN+user.getHead_url());
					json.put("d", profile);
				}
			}
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
